﻿using Protocol.Dto.Fight;
using UnityEngine;

namespace Script.Character
{
    public class CardCtrl : MonoBehaviour
    {
        // attribute
        public CardDto mCardDto;

        private SpriteRenderer spriteRenderer;

        // flag
        private bool isMine; // 是不是我的牌
        public bool Selected; // 卡牌是否被选中

        // Start is called before the first frame update
        private void Start()
        {
            Selected = false;
            spriteRenderer = transform.GetComponent<SpriteRenderer>();
        }


        public void Init(CardDto cardDto, int index, bool IsMine)
        {
            // TODO:
            // 覆盖原始数据
            mCardDto = cardDto;
            isMine = IsMine;
            Selected = false;

            transform.localPosition = new Vector3(0.2f * index, 0, 0);


            string resPath;

            if (IsMine)
            {
                resPath = "Poker/" + mCardDto.Name;
            }
            else
            {
                resPath = "Poker/CardBack";
            }

            Debug.Log(resPath);

            Sprite sprite = Resources.Load<Sprite>(resPath);
            Debug.Log("能加载到卡牌码" + sprite);

            spriteRenderer = transform.GetComponent<SpriteRenderer>();

            Debug.Log("spriteRenderer:" + spriteRenderer);

            if (spriteRenderer != null)
            {
                if (sprite != null)
                {
                    spriteRenderer.sprite = sprite;

                    spriteRenderer.sortingOrder = index;
                }
            }
        }


        /// <summary>
        /// 当鼠标点击的时候调用
        /// </summary>
        private void OnMouseDown()
        {
            if (isMine == false)
            {
                return;
            }

            if (Selected == false)
            {
                Selected = true;
                transform.localPosition += new Vector3(0, 0.3f, 0);
            }
            else
            {
                Selected = false;
                transform.localPosition -= new Vector3(0, 0.3f, 0);
            }

            Debug.Log("当前牌的数字" + mCardDto.Weight);
            Debug.Log("当前牌的是否选中" + Selected);
        }

        /// <summary>
        /// 选择的状态
        /// </summary>
        public void SelectState()
        {
            if (Selected == false)
            {
                Selected = true;
                transform.localPosition += new Vector3(0, 0.3f, 0);
            }
        }
    }
}